package cn.tianjindong.blog_manager.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import cn.tianjindong.blog.common.utils.VisiterInfoUtils;
import cn.tianjindong.blog.common.utils.MD5Utils;
import cn.tianjindong.blog.pojo.BlogManager;
import cn.tianjindong.blog.pojo.BlogManagerVisitlog;
import cn.tianjindong.blog_manager.service.BlogManagerService;
import cn.tianjindong.blog_manager.service.VisitLogService;

/**
 * 登录功能相关Controller
 * 
 * @author 田 金 东
 *
 */
@Controller
public class LoginController {
	@Autowired
	private BlogManagerService managerService;

	@Autowired
	private VisitLogService visitLogService;

	@RequestMapping("/login.html")
	public String toLoginPage() {
		return "login";
	}

	/**
	 * 登录操作
	 * 
	 * @param username
	 * @param password
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/login.action")
	public String login(HttpServletRequest request, HttpSession session, Model model, String username, String password)
			throws Exception {
		BlogManager manager = managerService.getManagerByUsername(username);
		// 获取请求者ip
		String ipAddress = VisiterInfoUtils.getIpAddress(request);
		BlogManagerVisitlog visitLog = new BlogManagerVisitlog();
		visitLog.setIpaddr(ipAddress);
		if (manager != null) {
			visitLog.setManagerId(manager.getId());
		}
		String md5Passwd = MD5Utils.md5(password);
		// 设置登录状态
		if (manager != null && manager.getPassword().equals(md5Passwd)) {
			visitLog.setStatus(1);// 1表示登录成功，0表示失败
		} else {
			visitLog.setStatus(0);
		}
		visitLogService.addManagerVisitLog(visitLog);

		if (manager != null && manager.getPassword().equals(md5Passwd)) {
			session.setAttribute("user", manager);
			return "redirect:index.html";
		} else {
			return "redirect:login.html?message=username_or_password_error";
		}
	}

	/**
	 * 进行session的刷新工作，防止session失效后，文章撰写页面的数据丢失
	 * 
	 * @return
	 */
	@RequestMapping("/session/update.action")
	public @ResponseBody String updateSession() {
		return "ok";
	}

	/**
	 * 密码重置操作
	 * 
	 * @return
	 */
	@RequestMapping("/repassword.action")
	public @ResponseBody String rePassword(BlogManager manager, String newpassword) {
		Map<String, String> result = new HashMap<>();
		BlogManager userData = managerService.getManagerByUsername(manager.getUsername());
		String md5Passwd = MD5Utils.md5(manager.getPassword());
		// 密码相同
		if (userData != null && userData.getPassword().equals(md5Passwd)) {
			manager.setPassword(MD5Utils.md5(newpassword));
			managerService.updateManager(manager);
			result.put("status", "ok");
			result.put("info", "更改成功");
		} else {
			result.put("status", "error");
			result.put("info", "密码错误");
		}
		return JSON.toJSONString(result);
	}

	/**
	 * 退出登录
	 * @return
	 */
	@RequestMapping("/logout.action")
	public @ResponseBody String logout(HttpSession session) {
		session.removeAttribute("user");
		return null;
	}
}
